<?php

namespace app\admin\controller;

use think\facade\Db;
use think\facade\Lang;
use think\facade\Env;
use think\facade\Cache;
use think\Request;

use app\common\controller\Backend;

/**
 * 公共的方法
 */
class Common extends Backend
{
    /**
     * 验证码
     */
    public function captcha(Request $request)
    {
        $param = $request->param();
        if (isset($param['api']) && $param['api'] == 0) {
            $param['api'] = false;
            return \app\common\service\Verify::entry($param);
        }

        $param['api'] = true;
        $captcha = \app\common\service\Verify::entry($param);
        return json([
            'code' => 200,
            'data' => [
                'key' => $captcha['key'],
                'src' => $captcha['data'],
                'switch' => true, // 是否启用
            ]
        ]);
    }

    /**
     * 清除缓存
     */
    public function delCache(Request $request)
    {
        // 清除文件缓存
        //$dirs = root_path() . 'runtime';
        //del_file($dirs);

        // 清理缓存
        // 清除admin标签的缓存数据
        Cache::tag('admin')->clear();

        //header('Location: ' . $_SERVER['HTTP_REFERER']);
        return json([
            'code' => 1,
            'msg' => '清除缓存成功！'
        ]);
    }
}
